using System;
using System.IO;
using System.Windows.Forms;
using BizTalk.BuildGenerator.MetaData;
using BizTalk.BuildGenerator.UI.Navigation;

namespace BizTalk.BuildGenerator.UI.UserControls
{
    /// <summary>
    /// Control for the build references
    /// </summary>
    public partial class ReferencesControl : UserControl, IWizardControl
    {
        private BuildReferences _References = new BuildReferences();

        /// <summary>
        /// Ctor
        /// </summary>
        public ReferencesControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Registers the build references to use
        /// </summary>
        /// <param name="refs"></param>
        public void RegisterReferences(BuildReferences refs)
        {
            _References = refs;
        }

        #region IWizardControl Members

        /// <summary>
        /// Implements i wizard control
        /// </summary>
        /// <returns></returns>
        public MoveNextResult MoveNext()
        {
            MoveNextResult result = new MoveNextResult();
            result.CanMoveNext = ValidateControl();
            return result;
        }

        #endregion

        /// <summary>
        /// Ensures the right data has been provided
        /// </summary>
        /// <returns></returns>
        private bool ValidateControl()
        {
            if (string.IsNullOrEmpty(_References.TasksPath) ||
                !Directory.Exists(_References.TasksPath))
            {
                MessageBox.Show("Please select a valid path for the  tasks");
                return false;
            }            

            return true;
        }

        /// <summary>
        /// Handles the load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReferencesControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
                BuildReferencesPropertyGrid.SelectedObject = _References;
        }
    }
}